package com.example.demo.controller.discuss.communication;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.demo.base.config.auth.UserInfoShareHolder;
import com.example.demo.base.login.dto.UserCache;
import com.example.demo.base.permission.dto.UserRoleDto;
import com.example.demo.domain.entity.Communication;
import com.example.demo.domain.entity.User;
import com.example.demo.dto.ResponseDto;
import com.example.demo.param.CommunicationParam;
import com.example.demo.service.CommunicationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * TODO
 * 帖子模块
 * @author mazh47622
 * @date2024/2/4
 * @modify -----------------------------------------------------------------------------------------------------
 * |修改单号|修改人员|修改日期|评审人员|修改说明
 * -----------------------------------------------------------------------------------------------------
 * |      |       |      |       |
 * -----------------------------------------------------------------------------------------------------
 */
@RequestMapping("/communication")
@RestController
public class CommunicationController {

    @Resource
    private CommunicationService communicationService;

    /**
     * 发布帖子
     * @param communicationParam
     * @return
     */

    @PostMapping("/createCommunication")
    ResponseDto createCommunication(@RequestBody CommunicationParam communicationParam){
        communicationService.save(communicationParam);
        return new ResponseDto<>().buildSucceedHasData(true);
    }


    /**
     * 查询帖子
     * @param param
     * @return
     */
    @PostMapping("/list")
    ResponseDto list(@RequestBody CommunicationParam param){
        return new ResponseDto<>().buildSucceedHasData(communicationService.list(param));
    }

    /**
     * 更新帖子点赞数量
     * @param
     * @return
     */

    @PostMapping("/updateCommunicationLikeNum")
    ResponseDto updateCommunicationLikeNum(@RequestBody CommunicationParam param){
        LambdaUpdateWrapper<Communication> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Communication::getId, param.getId());
        updateWrapper.set(Communication::getLikeNum, param.getLike());
        communicationService.update(updateWrapper);
        UserCache userInfo = UserInfoShareHolder.getUserInfo();
        User user = userInfo.getUser();
        System.out.println(user.toString());
        return new ResponseDto<>().buildSucceedHasData(true);
    }

    /**
     * 更新帖子收藏数量
     * @param
     * @return
     */

    @PostMapping("/updateCommunicationFavorNum")
    ResponseDto updateCommunicationFavorNum(@RequestBody CommunicationParam param){
        LambdaUpdateWrapper<Communication> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Communication::getId, param.getId());
        updateWrapper.set(Communication::getFavorNum, param.getFavor());
        communicationService.update(updateWrapper);
        return new ResponseDto<>().buildSucceedHasData(true);
    }

    /**
     * 删除帖子
     */
    @PostMapping("/delete")
    ResponseDto delete(@RequestBody CommunicationParam param){
        communicationService.delete(param);
        return new ResponseDto<>().buildSucceedHasData(true);
    }
}
